package com.kool.kadmin.system.service;

import com.kool.kadmin.common.bean.ListResult;
import com.kool.kadmin.common.bean.PageListResult;
import com.kool.kadmin.common.util.PaginatorUtil;
import com.kool.kadmin.common.util.ResultUtil;
import com.kool.kadmin.system.bean.SysParamDo;
import com.kool.kadmin.system.dao.SysParamDao;
import com.kool.kadmin.system.request.AddParamReq;
import com.kool.kadmin.system.request.GetParamPageReq;
import com.kool.kadmin.system.request.UpdateParamReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luyu
 */
@Service
@Slf4j
public class ParamService {
    @Resource
    private SysParamDao paramDao;

    /**
     * 增加
     *
     * @param request
     */
    @Transactional
    public void add(AddParamReq request) {
        SysParamDo paramDo = new SysParamDo();
        paramDo.setParamCode(request.getParamCode())
                .setFatherCode(request.getFatherCode())
                .setRootCode(request.getRootCode())
                .setParamName(request.getParamName())
                .setParamKey(request.getParamKey())
                .setParamValue(request.getParamValue())
                .setDeep(request.getDeep())
                .setSeq(request.getSeq());
        paramDao.insert(paramDo);
    }

    /**
     * 查询所有
     *
     * @return
     */
    public ListResult<SysParamDo> getAll() {
        return ResultUtil.success(paramDao.getAll());
    }

    /**
     * 修改
     *
     * @param request
     */
    @Transactional
    public void updateById(UpdateParamReq request) {
        SysParamDo paramDo = new SysParamDo();
        paramDo.setId(request.getId())
                .setFatherCode(request.getFatherCode())
                .setRootCode(request.getRootCode())
                .setParamName(request.getParamName())
                .setParamKey(request.getParamKey())
                .setParamValue(request.getParamValue())
                .setDeep(request.getDeep())
                .setSeq(request.getSeq());
        paramDao.updateById(paramDo);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Transactional
    public void deleteById(long id) {
        paramDao.deleteById(id);
    }

    /**
     * 根据参数码查询参数信息
     *
     * @param code
     * @return
     */
    public SysParamDo getParamByCode(String code) {
        return paramDao.getByCode(code);
    }

    /**
     * 查询父参数下所有子参数
     *
     * @param fatherCode
     * @return
     */
    public List<SysParamDo> getSubParams(String fatherCode) {
        return paramDao.getSubParams(fatherCode);
    }

    /**
     * 查询父参数下的key对应子参数
     *
     * @param fatherCode
     * @param key
     * @return
     */
    public SysParamDo getSubParam(String fatherCode, String key) {
        return paramDao.getSubParam(fatherCode, key);
    }

    /**
     * 更新子参数值
     *
     * @param fatherCode
     * @param key
     * @param value
     */
    @Transactional
    public void updateSubValue(String fatherCode, String key, String value) {
        paramDao.updateSubValue(fatherCode, key, value);
    }

    /**
     * 增加参数
     *
     * @param paramDo
     */
    @Transactional
    public void addParam(SysParamDo paramDo) {
        paramDao.insert(paramDo);
    }

    /**
     * 根据参数码更新参数
     *
     * @param paramDo
     */
    @Transactional
    public void updateByCode(SysParamDo paramDo) {
        paramDao.updateByCode(paramDo);
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public PageListResult<SysParamDo> getPage(GetParamPageReq request) {
        int currentPage = request.getCurrentPage();
        int pageSize = request.getPageSize();

        SysParamDo condition = new SysParamDo();
        condition.setParamCode(request.getParamCode())
                .setRootCode(request.getRootCode())
                .setFatherCode(request.getFatherCode());
        int total = paramDao.countTotal(condition);
        int offset = PaginatorUtil.calculateOffset(currentPage, pageSize);
        List<SysParamDo> list = paramDao.getPage(condition, offset, pageSize);
        return ResultUtil.success(list, currentPage, pageSize, total);
    }

    /**
     * 根据参数码删除参数
     *
     * @param paramCode
     */
    @Transactional
    public void deleteByCode(String paramCode) {
        paramDao.deleteByCode(paramCode);
    }
}
